package org.lyq.erp.auth.employee.controller;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.lyq.erp.auth.AuthContant;
import org.lyq.erp.auth.employee.pojo.EmployeeExcelModel;
import org.lyq.erp.auth.employee.service.EmployeeService;
import org.lyq.erp.config.readproperties.ReadProperties;
import org.lyq.erp.utils.string.ObjectEmptyUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
public class EmployeeExcelController {

	private static Logger logger = LoggerFactory.getLogger(ReadProperties.class);

	@Resource
	private EmployeeService employeeService;

	@SuppressWarnings("unchecked")
	@RequestMapping("/readEmployeeExcel")
	@ResponseBody
	private List<Object> readEmployeeExcel(@RequestParam("file") MultipartFile miageFile) throws IOException {

		List<Object> errorList = new ArrayList<Object>();
		InputStream inputStream = miageFile.getInputStream();

		// 判定上传的Excle文件是否为空和是否格式正确
		if (ObjectEmptyUtils.isNotEmpty(miageFile) && miageFile.getOriginalFilename().trim().length() > 0) {
			// 获取文件的后缀名称
			String originalFilename = miageFile.getOriginalFilename();
			String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
			if (!suffix.equals(".xls")) {
				errorList.add(AuthContant.ERROR_EXCELFILE_FORMAT);
				return errorList;
			}
		} else {
			errorList.add(AuthContant.ERROR_EXCELFILE_FORMAT);
			return errorList;
		}

		List<Object> temp = null;
		try {
			temp = (List<Object>) ReadEmployeeExcel.readXls(inputStream, errorList, EmployeeExcelModel.class);
			// 判定返回值类型来确定是否出现读取错误
			if (ObjectEmptyUtils.isNotEmptyList(temp)
					&& temp.get(0).getClass().getName().equals("org.apache.poi.hssf.usermodel.HSSFCell")) {
				return errorList;

			} else {
				EmployeeExcelModel employeeExcelModel = null;
				List<EmployeeExcelModel> employeeExcelModelList = new ArrayList<EmployeeExcelModel>();
				for (int i = 0; i < temp.size(); i++) {
					employeeExcelModel = (EmployeeExcelModel) temp.get(i);
					employeeExcelModelList.add(employeeExcelModel);
				}
				if (employeeExcelModelList != null && employeeExcelModelList.size() > 0) {

					// 循环判定数据库中是否存在相同的用户名
					for (int j = 0; j < employeeExcelModelList.size(); j++) {
						if (employeeService.checkName(employeeExcelModelList.get(j).getEmpUsername().trim())) {
							errorList.add("数据库中有相同的用户名，请修改第(" + (j + 3) + ")行中用户名为("
									+ employeeExcelModelList.get(j).getEmpUsername() + ")的用名字");
							return errorList;
						}
					}
					
					// 循环插入员工表中
					for (EmployeeExcelModel emp : employeeExcelModelList) {
						employeeService.insertEmployeeExcelModelSelective(emp);
					}
					
					errorList.add("上传成功！！一共上传(" + employeeExcelModelList.size() + ")条数据");
				} else {
					errorList.add("上传失败！！一共上传(" + 0 + ")条数据");
				}

			}
		} catch (Exception e) {

			logger.info(e.getMessage());
		} finally {
			if (inputStream != null)
				inputStream.close();
		}
		return errorList;
	}

}
